/*
 * Copyright (c) 2018 Nanjing Wind Information Co., Ltd. All Rights Reserved;
 * @Package com.wind.autotest.web.controll
 * @version 1.0.0
 * @Date 2018年12月4日
 * @author jlliu.Farrior@wind.com.cn
 */
package wind.autotest.web;

import java.io.File;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import wind.autotest.pojo.TestTable;
import wind.autotest.pojo.TestTask;
import wind.autotest.service.frontend.TestTableService;
import wind.autotest.service.frontend.TestTaskService;
import wind.autotest.util.Page4Navigator;

/**
 * @Description: <任务配置后台控制类>
 * @author jlliu.Farrior@wind.com.cn
 * @Date 2018年12月4日 下午4:37:13
 * 
 */
@RestController
public class TaskListController {
	
	@Autowired
	private TestTaskService taskService;
	@Autowired
	private TestTableService tableService;
	
	/**
	 * 
	 * 功能描述:<显示任务列表>
	 * @param start
	 * @param size
	 * @return
	 * @throws Exception 
	 * @author jlliu.Farrior
	 */
	@GetMapping("/tasks")
	public Page4Navigator<TestTask> list(@RequestParam(value = "start", defaultValue = "0") int start, @RequestParam(value = "size", defaultValue = "5") int size) throws Exception {
		start = start < 0 ? 0 : start;
		Page4Navigator<TestTask> page =taskService.list(start, size, 5);
		return page;
	}
	
	/**
	 * 
	 * 功能描述:<新增任务>
	 * @param bean
	 * @return
	 * @throws Exception 
	 * @author jlliu.Farrior
	 */
	@PostMapping("/tasks")
	public Object add(TestTask bean) throws Exception {
		taskService.add(bean);
		// 添加至任务配置表
//		autoTaskMapper.insertTask(bean);
		// 添加至任务执行状态表
//		TaskStatusDTO taskStatus = new TaskStatusDTO(String.valueOf(taskid),bean.getTaskName(),"0","0");
//		taskStatusMapper.insertTaskStatus(taskStatus);
		return bean;
	}
	
	@DeleteMapping("/tasks/{id}")
	public String delete(@PathVariable("id") int id, HttpServletRequest request)  throws Exception {
		TestTask task = taskService.get(id);
		taskService.delete(id);
		// TODO 删除配置表 暂时不做
//		tableService.deleteByTask(task);
		
		return null;
	}

	@GetMapping("/tasks/{id}")
	public TestTask get(@PathVariable("id") int id) throws Exception {
		TestTask bean = taskService.get(id);
		return bean;
	}

	@PutMapping("/tasks/{id}")
	public Object update(TestTask bean,HttpServletRequest request) throws Exception {
		String name = request.getParameter("name");
		bean.setName(name);
		taskService.update(bean);
		return bean;
	}
	
	
}
